package com.lb.bs.client.config;

import com.lb.bs.client.adapter.enums.SerializeType;
import com.lb.bs.client.adapter.enums.StarterType;
import com.lb.bs.client.util.IPUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-11-27 11:00
 * description:
 */
public class StaticConfig {
    private static final Logger logger = LoggerFactory.getLogger(StaticConfig.class);

    private String scanPackages;

    private String zkHost;

    private String nameSpace;

    private StarterType starterType = StarterType.SPRING;

    private SerializeType serializeType = SerializeType.JDK;
    /**
     *
     * 如果在配置里配置了多个日志节点，
     *
     * 将以该节点作为唯一的日志节点，如果不指定将抛出异常
     *
     * 该节点是需要加上groupName的
     *
     *
     */
    private String activeLogPath;

    public StaticConfig(Builder builder) {

        this.scanPackages = builder.scanPackages;

        this.zkHost = builder.zkHost;

        this.starterType = builder.starterType;

        boolean isIpAddress = IPUtil.isIPAddress(zkHost);

        if (!isIpAddress) {
            this.zkHost = IPUtil.getUrlByHost(zkHost);
        }
        logger.info("StaticConfig.class,isIpAddress:{},zkHost:{}", isIpAddress, zkHost);

        this.nameSpace = builder.nameSpace;

        this.serializeType = builder.serializeType;

        this.activeLogPath = builder.activeLogPath;
    }

    public static Builder builder() {

        return new Builder();
    }

    public static class Builder {

        private String scanPackages;

        private String zkHost;

        private String nameSpace;

        private StarterType starterType = StarterType.SPRING;

        private SerializeType serializeType = SerializeType.JDK;

        private String activeLogPath;



        public Builder scanPackages(String scanPackages) {
            this.scanPackages = scanPackages;
            return this;
        }


        public Builder zkHost(String zkHost) {
            this.zkHost = zkHost;
            return this;
        }


        public Builder namespace(String nameSpace) {
            this.nameSpace = nameSpace;
            return this;
        }


        public Builder starterType(StarterType starterType) {
            this.starterType = starterType;
            return this;
        }

        public Builder serializeType(SerializeType serializeType) {
            this.serializeType = serializeType;
            return this;
        }

        public Builder activeLogPath(String activeLogPath) {
            this.activeLogPath = activeLogPath;
            return this;
        }
        public StaticConfig build() {
            return new StaticConfig(this);
        }
    }

    public String getScanPackages() {
        return scanPackages;
    }

    public String getZkHost() {
        return zkHost;
    }

    public String getNameSpace() {
        return nameSpace;
    }

    public StarterType getStarterType() {
        return starterType;
    }

    public SerializeType getSerializeType() {
        return serializeType;
    }

    public String getActiveLogPath() {
        return activeLogPath;
    }
}
